package com.bus.core.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import com.bus.common.jpa.Finder;
import com.bus.common.jpa.JpaBaseDao;
import com.bus.common.page.Pagination;
import com.bus.core.dao.BusUserEntityDao;
import com.bus.core.entity.BusUser;

@CacheConfig(cacheNames="BusUserEntity")
@Repository
public class BusUserEntityDaoImpl extends JpaBaseDao<BusUser, Integer> implements
		BusUserEntityDao {
	
	@Cacheable
	public Pagination getPage(String username, String email, Integer siteId,
			Integer groupId, Integer statu, Boolean admin, Integer rank,
			String realName, Integer roleId, Boolean allChannel,
			Boolean allControlChannel, int pageNo, int pageSize) {
		Finder f = Finder
				.create("select bean from BusUser bean join bean.userExtSet ext ");
		if (siteId != null || allChannel != null || allControlChannel != null) {
			f.append(" join bean.userSites userSite");
		}
		if (roleId != null) {
			f.append(" join bean.roles role ");
		}
		f.append(" where 1=1");
		if (siteId != null) {
			f.append(" and  userSite.site.id=:siteId");
			f.setParam("siteId", siteId);
		}
		if (!StringUtils.isBlank(username)) {
			f.append(" and bean.username like :username");
			f.setParam("username", "%" + username + "%");
		}
		if (!StringUtils.isBlank(email)) {
			f.append(" and bean.email like :email");
			f.setParam("email", "%" + email + "%");
		}
		if (groupId != null) {
			f.append(" and bean.group.id=:groupId");
			f.setParam("groupId", groupId);
		}
		if (statu != null) {
			f.append(" and bean.statu=:statu");
			f.setParam("statu", statu);
		}
		if (admin != null) {
			f.append(" and bean.admin=:admin");
			f.setParam("admin", admin);
		}
		if (rank != null) {
			f.append(" and bean.rank<=:rank");
			f.setParam("rank", rank);
		}
		if (!StringUtils.isBlank(realName)) {
			f.append(" and ext.realname like :realname");
			f.setParam("realname", "%" + realName + "%");
		}
		if (roleId != null) {
			f.append(" and role.id=:roleId");
			f.setParam("roleId", roleId);
		}
		if (allChannel != null) {
			f.append(" and userSite.allChannel=:allChannel");
			f.setParam("allChannel", allChannel);
		}
		if (allControlChannel != null) {
			f.append(" and userSite.allChannelControl=:allControlChannel");
			f.setParam("allControlChannel", allControlChannel);
		}
		// 用户有多个站的管理权限需要去重复
		/*
		 * if(allChannel!=null||allControlChannel!=null){
		 * f.append(" group by bean having count(bean)=1"); }
		 */
		f.append(" order by bean.id desc");
		return find(f, pageNo, pageSize);
	}

	@SuppressWarnings("unchecked")
	@Cacheable
	public List<BusUser> getList(String username, String email, Integer siteId,
			Integer groupId, Integer statu, Boolean admin, Integer rank) {
		Finder f = Finder.create("select bean from BusUser bean");
		if (siteId != null) {
			f.append(" join bean.userSites userSite");
			f.append(" where userSite.site.id=:siteId");
			f.setParam("siteId", siteId);
		} else {
			f.append(" where 1=1");
		}
		if (!StringUtils.isBlank(username)) {
			f.append(" and bean.username like :username");
			f.setParam("username", "%" + username + "%");
		}
		if (!StringUtils.isBlank(email)) {
			f.append(" and bean.email like :email");
			f.setParam("email", "%" + email + "%");
		}
		if (groupId != null) {
			f.append(" and bean.group.id=:groupId");
			f.setParam("groupId", groupId);
		}
		if (statu != null) {
			f.append(" and bean.statu=:statu");
			f.setParam("statu", statu);
		}
		if (admin != null) {
			f.append(" and bean.admin=:admin");
			f.setParam("admin", admin);
		}
		if (rank != null) {
			f.append(" and bean.rank<=:rank");
			f.setParam("rank", rank);
		}
		f.append(" order by bean.id desc");
		return find(f);
	}


	public BusUser findById(Integer id) {
		BusUser entity = get(id);
		return entity;
	}


	public int countByUsername(String username) {
		String hql = "select count(*) from BusUser bean where bean.username=:username";
		Query query = getEntityManager().createQuery(hql);
		query.setParameter("username", username);
		return ((Number) query.getSingleResult()).intValue();
	}

	public int countMemberByUsername(String username) {
		String hql = "select count(*) from BusUser bean where bean.username=:username and bean.admin=false";
		Query query = getEntityManager().createQuery(hql);
		query.setParameter("username", username);
		return ((Number) query.getSingleResult()).intValue();
	}

	public int countByEmail(String email) {
		String hql = "select count(*) from BusUser bean where bean.email=:email";
		Query query = getEntityManager().createQuery(hql);
		query.setParameter("email", email);
		return ((Number) query.getSingleResult()).intValue();
	}

	@CacheEvict
	public BusUser save(BusUser bean) {
		getEntityManager().persist(bean);
		return bean;
	}

	@CacheEvict
	public BusUser deleteById(Integer id) {
		BusUser entity = super.get(id);
		if (entity != null) {
			getEntityManager().remove(entity);
		}
		return entity;
	}

	@Override
	protected Class<BusUser> getEntityClass() {
		return BusUser.class;
	}

	@Override
	public List<BusUser> getAdminList(Integer siteId, Boolean allChannel,
			Integer statu, Integer rank) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pagination getAdminsByRoleId(Integer roleId, int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}
}